package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色分页查询
     * @param pageNum
     * @param pageSize
     * @param roleDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> getRoleList(@PathVariable Integer pageNum,
                                                            @PathVariable Integer pageSize,
                                                            @RequestBody RoleDto roleDto){
        log.info("角色分页查询接口，入参pageNum={},pageSize={},roleDto={}",pageNum,pageSize,roleDto);
        PageResponse<RoleVo> pageResponse = roleService.getRoleList(pageNum,pageSize,roleDto);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 根据角色id查询关联的资源编号
     * @param roleId
     * @return
     */
    @GetMapping("find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> getRoleResourceById(@PathVariable Long roleId){
        log.info("根据角色id查询关联的资源编号接口，入参roleId={}",roleId);
        Set<String> data = roleService.getRoleResourceById(roleId);
        return ResponseResult.success(data);
    }

    /**
     * 添加角色
     * @param roleDto
     * @return
     */
    @PutMapping
    public ResponseResult addRole(@RequestBody RoleDto roleDto){
        log.info("添加角色接口，入参={}",roleDto);
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 角色修改
     * @param roleDto
     * @return
     */
    @PatchMapping
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){
        log.info("角色修改接口，入参={}",roleDto);
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    public ResponseResult deleteRole(@PathVariable Long roleId){
        log.info("删除角色接口，入参id={}",roleId);
        roleService.deleteRole(roleId);
        return ResponseResult.success();
    }

    /**
     * 角色下拉框,查询全部角色
     * @return
     */
    @PostMapping("/init-roles")
    public ResponseResult<List<Role>> getAllRole(){
        log.info("获取全部角色接口");
        List<Role> roles = roleService.getAllRole();
        return ResponseResult.success(roles);
    }

}
